package icu.sehnen.controller;


import com.github.pagehelper.PageInfo;
import icu.sehnen.common.CommonResult;
import icu.sehnen.mapper.NewsMapper;
import icu.sehnen.model.NewsCategoryModel;
import icu.sehnen.model.NewsModel;
import icu.sehnen.model.ProductModel;
import icu.sehnen.service.NewsCategoryService;
import icu.sehnen.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/news")
public class NewsController {
    @Autowired
    NewsService newsService;
    @Autowired
    NewsCategoryService newsCategoryService;
    @GetMapping("/list")
    public  CommonResult findAll(Integer pageNum,Integer pageSize,String query){
        PageInfo<NewsModel> newsModelList=newsService.findAll( pageNum, pageSize,query);
        return  CommonResult.success(newsModelList);

    }
    @GetMapping("/category")
    public  CommonResult findCategory(){
      List<NewsCategoryModel> newsCategoryModelList=   newsCategoryService.findAll();
        return  CommonResult.success(newsCategoryModelList);
    }
    @PostMapping("/add")
    public  CommonResult addNews(NewsModel newsModel){
         newsService.add(newsModel);
        return  CommonResult.success();
    }
    @PostMapping("/update")
    public  CommonResult update(NewsModel newsModel){
        newsService.update(newsModel);
        return  CommonResult.success();
    }
    @GetMapping("/find/{id}")
    public  CommonResult find(@PathVariable Long id){
       NewsModel newsModel= newsService.find(id);
       return  CommonResult.success(newsModel);
    }
    @PostMapping("/delete/{id}")
    public  CommonResult delete(@PathVariable Long id){
        newsService.delete(id);
        return  CommonResult.success();
    }

}
